package dbutils;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.junit.Test;

import java.sql.SQLException;

public class Dbutils_Demo1 {

    /*
   dbutils增删改
      1、QueryRunner qr= new  QueryRunner(连接池对象); //无事务操作
      2、update(sql,...obj) 无事务操作


      2、QueryRunner qr =new QueryRunner() //有事务的操作
      update(con,sql,...obj) 事务操作

  */
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        QueryRunner qr = new QueryRunner(dataSource); //可执行对象
    //增
    @Test
    public void  insertAccount() throws SQLException {
           String sql = "insert into account values(?,?,?)";
           //可以把...可变参数 ,作为一个数组传入
//           Object[] arr ={7,"sssb",1800};
//            System.out.println(qr.update(sql,arr) > 0? "添加成功":"添加失败");
        System.out.println(qr.update(sql,7,"满意",5200) > 0? "添加成功":"添加失败");
        }
    //删
    @Test
    public void deleteAccount() throws SQLException {
        String sql =" delete  from account where id=?";
        System.out.println(qr.update(sql, 7) > 0 ? "删除成功" : "删除失败");
    }
    //改
    @Test
    public void updateAccount() throws SQLException {
        String sql = "update account set name = ? where id = ? ";
        System.out.println(qr.update(sql, "王亚小", 5) > 0 ? "修改成功" : "修改失败");
    }
}
